home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-17 | 18.0 KB | 595 lines | [TEXT/MMCC] |
- // QuickTest.cp Based on npshell.cp by NetScape Communications
- // by Keith McGlauflin Portions copyright 1996 Netscape Communications
- //
- // This Netscape Navigator plugin displays an indicator strip based on the
- // contents of the .test file referenced in the HTML file. Controls are
- // displayed to increase and decrease the indicator's level.
-
- #ifndef _NPAPI_H_
- #include "npapi.h" // Include Netscape's header file
- #endif
-
- #define kMax 20 // Maximum indicator value
- #define kMin 1 // Minimum indicator value
- #define kDecrease "\pDecrease" // Decrease button text
- #define kIncrease "\pIncrease" // Increase button text
- #define kBufferSize 1 // Size of the read buffer in bytes
-
- // Plugin Instance Data
-
- typedef struct _PluginInstance // Data structure to hold all our variable for
- { // This plug-in instance
- NPWindow* fWindow; // Netscape Plugin Window record
- uint16 fMode; // Plugin's mode (Full screen, embedded, or background)
- Boolean amBusy; // Are we loading data?
- Ptr data; // Pointer to our data read from the .test file
- int datalength; // Length of the data in Ptr
- Byte min; // Indictator's minimum value
- Byte max; // Indictator's maximum value
- Byte current; // Indictator's current value
- ControlHandle decControl; // Handle to the Decrement control
- ControlHandle incControl; // Handle to the Increment control
- } PluginInstance;
-
- // Our plugin's globals:
-
- CGrafPort gSavePort; // Saved Port setting
- CGrafPtr gOldPort; // Original Port settings
- short gRFRN; // Resource fork reference number for this plugin's resource fork
- Handle gonPict; // Handle to the indicator's ON pict
- Handle goffPict; // Handle to the indicator's OFF pict
-
- // Prototype functions:
-
- Boolean SavePort( NPWindow *window );
- void RestorePort( NPWindow *window );
- void DrawContents( PluginInstance *This );
- void HandleContents( PluginInstance *This, Point where, WindowPtr theWindow );
- void AddControls( PluginInstance *This );
- void UpdateCntrls( NPWindow *window );
- void GetData( PluginInstance *This, unsigned long len, void *buffer );
- void SetValues( PluginInstance *This );
- void SetDefaults( PluginInstance *This );
- int GetValue( Ptr bufferstart );
-
- // NPP_Initialize: (from npshell.cp) This procedure sets the clip region for our
- // gSavePort global, creates an FSSpec for our plugin, opens the resource fork
- // for our plugin, creates the gonPict and goffPict ControlHandles, and returns
- // NPERR_NO_ERROR.
-
- NPError NPP_Initialize(void)
- {
- gSavePort.clipRgn = ::NewRgn();
-
- gRFRN = CurResFile(); // Get the plug-in's resource file
- if ( ResError( ) == noErr )
- {
- gonPict = GetResource( 'PICT', 3000 ); // Get the ON pict
- goffPict = GetResource( 'PICT', 3001 ); // Get the OFF pict
- }
-
- DetachResource( gonPict );
- DetachResource( goffPict );
- return NPERR_NO_ERROR;
- }
-
-
- // NPP_Shutdown: (from npshell.cp) This procedure disposes of our clip region in
- // gSavePort and releases the indicator's ON and OFF pict handles.
-
- void NPP_Shutdown(void )
- {
- if (gSavePort.clipRgn)
- ::DisposeRgn(gSavePort.clipRgn);
-
- ReleaseResource( gonPict ); // Release the on Pict's resource
- ReleaseResource( goffPict ); // Release the off Pict's resource
- }
-
-
- // NPP_New: (from noshell.cp) This routine creates a new plugin instance. First the
- // plugin validates the instance we received from Navigator, then it allocates enough
- // memory to hold the PluginInstance data structure. Finally, the plugin sets all
- // the variables of our instance to their initial state.
-
- NPError NPP_New(NPMIMEType pluginType,
- NPP instance,
- uint16 mode,
- int16 argc,
- char* argn[],
- char* argv[],
- NPSavedData* saved)
- {
- if (instance == NULL) // Check for invalid plugin instance
- return NPERR_INVALID_INSTANCE_ERROR;
-
-
- instance->pdata = NPN_MemAlloc(sizeof(PluginInstance));
- PluginInstance* This = (PluginInstance*) instance->pdata;
- if (This != NULL)
- { // Initialize our plugin instance's variables
- This->fWindow = NULL; // No window assigned yet
- This->amBusy = FALSE; // Not currently loading data
- This->data = NULL; // Data pointer is NULL
- This->datalength =0; // Length of data is zero
- This->min = 0; // Min, max and current are set
- This->max = 0; // to zero
- This->current =0;
- This->decControl = NULL; // ControlHandles set to NULL
- This->incControl = NULL;
-
- return NPERR_NO_ERROR;
- }
- else
- return NPERR_OUT_OF_MEMORY_ERROR; // Couldn't get enough memory
- }
-
-
-
- // NPP_Destroy: ( from npshell.cp ) This procedure destroys a PluginInstance. First
- // the data pointer's memory is freed, then the controls are destroyed, and finally
- // the instance itself is freed and set to zero (so that it won't be errantly used again).
-
- NPError NPP_Destroy(NPP instance, NPSavedData** save)
- {
- if (instance == NULL) // Check for invalid plugin instance
- return NPERR_INVALID_INSTANCE_ERROR;
-
- PluginInstance* This = (PluginInstance*) instance->pdata;
-
- if (This != NULL)
- {
- if (This->data)
- NPN_MemFree(This->data); // Free up data pointer
-
- if ( This->decControl != NULL ) // Destroy the controls
- DisposeControl( This->decControl );
- if ( This->incControl != NULL )
- DisposeControl( This->incControl );
-
- NPN_MemFree(instance->pdata); // Free instance data
- instance->pdata = NULL; // Set instance to NULL
- }
-
- return NPERR_NO_ERROR;
- }
-
-
-
-
- // NPP_SetWindow: ( from npshell.cp ) This procedure sets our PluginInstance's window
- // to the window passed as a parameter in this procedure.
-
- NPError NPP_SetWindow(NPP instance, NPWindow* window)
- {
- if (instance == NULL)
- return NPERR_INVALID_INSTANCE_ERROR;
-
- PluginInstance* This = (PluginInstance*) instance->pdata;
-
- This->fWindow = window; // Set this instance window to the NPwindow
-
- return NPERR_NO_ERROR;
- }
-
-
-
-
- // NPP_NewStream: (from npshell.cp) Sets the amBusy boolan to true, indicating that
- // we are ready to load data from the .test file.
-
- NPError NPP_NewStream(NPP instance,
- NPMIMEType type,
- NPStream *stream,
- NPBool seekable,
- uint16 *stype)
- {
- if (instance == NULL)
- return NPERR_INVALID_INSTANCE_ERROR;
- PluginInstance* This = (PluginInstance*) instance->pdata;
-
- This->amBusy = TRUE; // Loading data...
-
- return NPERR_NO_ERROR;
- }
-
-
-
- // NPP_WriteReady: ( from npshell.cp ) Returns the value of our read buffer.
- // (Navigator will be doing the writing, our plugin will be reading.)
-
- int32 NPP_WriteReady(NPP instance, NPStream *stream)
- {
- return kBufferSize; // Return our buffersize
- }
-
-
-
-
- // NPP_Write: ( from npshell.cp ) Loads the data from the .test file into the
- // PluginInstance.
-
- int32 NPP_Write(NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
- {
- if (instance == NULL) // Check for invalid plugin instance
- return NPERR_INVALID_INSTANCE_ERROR;
-
- PluginInstance* This = (PluginInstance*) instance->pdata;
-
- if (This != NULL)
- GetData(This, len, buffer); // Load the .test file's data into
- // This->data
- return 0;
- }
-
-
-
-
- // NPP_DestroyStream: ( from npshell.cp ) When finished reading data, the plugin will
- // set amBusy to false, set the min, max and current values of our PluginInstance,
- // draw the controls, and draw the indicator.
-
- NPError NPP_DestroyStream(NPP instance, NPStream *stream, NPError reason)
- {
- if (instance == NULL) // Check for invalid plugin instance
- return NPERR_INVALID_INSTANCE_ERROR;
-
- PluginInstance* This = (PluginInstance*) instance->pdata;
-
- This->amBusy = FALSE; // Finished loading...
-
- if (SavePort(This->fWindow)) // Save the current GrafPort
- {
- SetValues( This ); // Set min, max and current
- AddControls( This ); // Draw the controls
- DrawContents(This); // Draw the indicator
- RestorePort(This->fWindow); // Restore the old GrafPort
- }
-
- return NPERR_NO_ERROR;
- }
-
-
-
- // NPP_StreamAsFile: ( from npshell.cp )
-
- void NPP_StreamAsFile(NPP instance, NPStream *stream, const char* fname)
- {
- // QuickTest doesn't suppose loading files
- }
-
-
-
- // NPP_HandleEvent: ( from npshell.cp ) Takes a pointer to an event, casts it to
- // an EventRecord* and handles the event. This plugin only handles update and
- // mousedown events.
-
- int16 NPP_HandleEvent(NPP instance, void* event)
- {
- Boolean eventHandled = false; // Has the event been processed?
- WindowPtr theWindow; // Window where mouse was pressed
- short wherePressed; // Coordinates where mouse was pressed
- EventRecord *ev; // Event record for this event
- NP_Port *port; // The window for mouse click
-
- if (instance == NULL) // Check for invalid plugin instance
- return eventHandled;
-
- PluginInstance* This = (PluginInstance*) instance->pdata;
- if (This != NULL && event != NULL)
- {
- ev = (EventRecord*) event; // Convert the event to an EventRecord
- switch (ev->what) // Get the type of event
- {
- case updateEvt: // Update event:
- if( SavePort( This->fWindow ) ) // Save the current GrafPort
- {
- DrawContents(This); // Draw the contents
- UpdateCntrls( This->fWindow ); // Update the controls
- RestorePort( This->fWindow ); // Restore the old GrafPort
- }
- eventHandled = true; // Event was processed
- break;
-
- case mouseDown: // Mouse down:
- port = (NP_Port*) This->fWindow->window; // Get the GrafPort from the
- theWindow = ( WindowPtr ) port->port; // fWindow data structure
- if ( theWindow != FrontWindow() ) // If window isn't front window
- BringToFront( theWindow ); // bring it to the front.
- else
- {
- wherePressed = FindWindow( ev->where, &theWindow );
- // Find where mouse down
- switch ( wherePressed ) // If mouse press down in...
- {
- case inContent: // ...content...
- if( SavePort( This->fWindow ) ) // Save current GrafPort
- {
- HandleContents( This, ev->where, theWindow );
- // Handle the mouse down event
- RestorePort( This->fWindow );
- // Restore the old GrafPort
- }
- break;
- }
- }
- break;
-
- default:
- break;
- }
-
- }
-
- return eventHandled; // Let Navigator know if we processed the event
- }
-
-
-
- // NPP_Print: ( from npshell.cp ) For brevity this procedure isn't implemented.
- // The code in this procedure does the minimal processing required to make printing
- // work (note that the plugin's indicator and controls are not printed - see the
- // examples in the Plugin SDK for information on printing within plugins).
-
- void NPP_Print(NPP instance, NPPrint* printInfo)
- {
- if (instance != NULL)
- {
- if (printInfo->mode == NP_FULL) // in fullscreen mode we don't do anything
- // for printing.
- printInfo->print.fullPrint.pluginPrinted = false;
- }
- }
-
-
- // NPP_URLNotify: Not implemented
-
- void NPP_URLNotify(NPP instance, const char* url,NPReason reason, void* notifyData)
- {
- // QuickTest doesn't implement this method
- }
-
-
- // NPP_GetJavaClass: Not Implemented (NOTE: this gives a warning during Make)
-
- jref NPP_GetJavaClass(void)
- {
- // QuickTest doesn't implement this method
- }
-
- // SavePort: Since Mac plugins share the drawing environment with Navigator we MUST
- // save the current GrafPort settings. This subroutine saves the current port's
- // clipping rectangle. Save other port settings before you change them.
-
- Boolean SavePort(NPWindow *window)
- {
- Rect clipRect;
- NP_Port* port;
- if (window == NULL)
- return FALSE;
-
- port = (NP_Port*) window->window;
- if (window->clipRect.left < window->clipRect.right)
- {
- // Preserve the old port
- ::GetPort((GrafPtr*)&gOldPort);
- ::SetPort((GrafPtr)port->port);
-
- // Preserve the old drawing environment
- gSavePort.portRect = port->port->portRect;
- ::GetClip(gSavePort.clipRgn);
-
- // Setup our drawing environment
-
- clipRect.top = window->clipRect.top + port->porty;
- clipRect.left = window->clipRect.left + port->portx;
- clipRect.bottom = window->clipRect.bottom + port->porty;
- clipRect.right = window->clipRect.right + port->portx;
- ::SetOrigin(port->portx,port->porty);
- ::ClipRect(&clipRect);
- clipRect.top = clipRect.left = 0;
- return TRUE;
- }
- else
- return FALSE;
- }
-
-
-
- // RestorePort: restore the old port settings so Navigator has the same GrafPort setting
-
- void RestorePort(NPWindow *window)
- {
- NP_Port* port;
- CGrafPtr myPort;
-
- port = (NP_Port*) window->window;
- ::SetOrigin(gSavePort.portRect.left, gSavePort.portRect.top);
- ::SetClip(gSavePort.clipRgn);
-
- ::GetPort((GrafPtr*)&myPort);
- ::SetPort((GrafPtr)gOldPort);
- }
-
-
-
- // DrawContents: draw the indicator. Indicator is made up of on and off Picts which
- // must be loaded from the plugin's resource fork.
-
- void DrawContents(PluginInstance *This)
- {
- Rect drawRect; // Drawing rectangle
- short loop; // Loop to process indicator
-
- drawRect.top = 0; // Set the initial draw rectangle
- drawRect.bottom = 34;
- drawRect.left = 0;
- drawRect.right = 17;
-
- for ( loop = This->min; loop <= This->current; loop++ ) // Draw the ON picts for
- { // the indicator
- ::DrawPicture( (PicHandle) gonPict, &drawRect );
- drawRect.left += 23; // Move the draw rectangle
- drawRect.right = drawRect.left + 17; // to the right
- }
-
- for ( loop = This->current+1; loop <= This->max; loop++ ) // Draw OFF picts for
- { // the indicator
- ::DrawPicture( (PicHandle) goffPict, &drawRect );
- drawRect.left += 23; // Move the draw rectangle
- drawRect.right = drawRect.left + 17; // to the right
- }
-
- }
-
-
- // HandleContents: process mouse clicks in the contents of the window. Track clicks
- // in the controls and process those clicks.
-
- void HandleContents( PluginInstance *This, Point where, WindowPtr theWindow )
- {
- int part, thePart; // part mouse down occurred
- ControlHandle theControl; // Control mouse was pressed in
- Str255 theTitle; // Control's title
-
- GlobalToLocal( &where ); // Convert coordinates
- part = FindControl( where, theWindow, &theControl ); // Find control were mouse
- // down occurred
- if ( theControl != NULL ) // If control valid
- {
- thePart = TrackControl( theControl, where, nil ); // Track the press
- if ( thePart == inButton ) // If release in button
- {
- ::GetControlTitle( theControl, theTitle ); // Get the title of the control
- if ( EqualString( theTitle, kDecrease, false, false ) )
- { // If decrease title...
- This->current--; // Decrease indicator's current
- // value
- if ( This->current < This->min ) // Make sure current >= min
- {
- This->current = This->min; // Make current = min
- ::SysBeep( 10 );
- }
- DrawContents( This ); // Draw the contents
- }
- if ( EqualString( theTitle, kIncrease, false, false ) )
- { // If increase title...
- This->current++; // Increase indicator;s current
- // value
- if ( This->current > This->max ) // Make sure current <= max
- {
- This->current = This->max; // Make current = max
- ::SysBeep(10);
- }
- DrawContents( This ); // Draw the contents
- }
- }
- }
- }
-
-
- // UpdateCntrls: updates the controls for the window.
-
- void UpdateCntrls( NPWindow *window )
- {
- WindowPtr theWindow; // Window pointer for window which contains controls
- NP_Port *port; // NP_port which points to GrafPort
-
- port = ( NP_Port* ) window->window; // Convert fWindow
- theWindow = ( WindowPtr ) port->port; // to a WindowPtr
-
- UpdateControls( theWindow, theWindow->visRgn ); // Update the window's controls
- }
-
-
- // GetData: Get the data out of the buffer and put it into the data pointer of
- // our plugin instance.
-
- void GetData( PluginInstance *This, unsigned long len, void *buffer )
- {
- char *newText;
- long offset;
-
- if (This->data == NULL) // No data loaded yet
- {
- newText = (char*) NPN_MemAlloc(len); // Allocate newText
- This->datalength = 0; // Set length of data
- offset = 0; // Set initial offset
- }
- else // We've loaded data
- {
- newText = (char*) NPN_MemAlloc(This->datalength+len);
- BlockMove(This->data, newText, This->datalength);
- NPN_MemFree(This->data);
- offset = This->datalength;
- }
- BlockMove(buffer, newText+This->datalength, len); // Move buffer data
- This->data = newText; // info data
- This->datalength += len; // and set the length
- }
-
-
- // SetValues: convert data to min, max and current values. If values are out
- // of bounds then set min, max and current to default values.
-
- void SetValues( PluginInstance *This )
- {
- This->min = GetValue( This->data ); // Set min
- This->max = GetValue( ( This->data ) + 7 ); // Set max
- This->current = GetValue( ( This-> data ) + 14 ); // set current
-
- if ( ( This->min > This->current ) || ( This->max < This->current ) )
- SetDefaults( This ); // Use default if current too low or too high
-
- if ( ( This->min < kMin ) || ( This->min > kMax ) )
- SetDefaults( This ); // Use default if min too low or too high
-
- if ( ( This->max < kMin ) || ( This->max > kMax ) )
- SetDefaults( This ); // Use default if max too low or too high
-
- if ( This->max < This->min )
- SetDefaults( This ); // Use default if min greater than max
- }
-
-
- // SetDefaults: If min, max or current out of range set all three to defaults
-
- void SetDefaults( PluginInstance *This )
- {
- This->min = kMin;
- This->max = kMax;
- This->current = kMin;
- }
-
-
- // GetValues: read three bytes starting at bufferstart+4 ( to skip over the
- // min=, max=, or cur= ) and convert it to an integer.
-
- int GetValue( Ptr bufferstart )
- {
- int value = 0;
-
- bufferstart = bufferstart + 4;
- value = ( ( *(bufferstart) - 48 ) * 100 ) +
- ( ( *(bufferstart + 1 ) - 48 ) * 10 ) + ( *(bufferstart + 2 ) - 48 );
- return value;
- }
-
-
- // AddControls: add the controls for increase and decrease to the window
-
- void AddControls( PluginInstance *This )
- {
- Rect drawRect;
- WindowPtr theWindow;
- NP_Port *port;
-
- port = (NP_Port*) This->fWindow->window; // Convert the fWindow
- theWindow = ( WindowPtr ) port->port; // to a window pointer
-
- SetRect( &drawRect, 10, 46, 100, 71 ); // Rect for the decrement control
- This->decControl = NewControl( theWindow, &drawRect, "\pDecrease", true, 0, 0, 1, pushButProc, 0);
-
- SetRect( &drawRect, 120, 46, 210, 71 ); // Rect for the increment control
- This->incControl = NewControl( theWindow, &drawRect, "\pIncrease", true, 0, 0, 1, pushButProc, 1);
- }